package service

import (
	"encoding/json"
	"net/http"
	"io/ioutil"
	"api/util"
)

func GetMakerAuthInfoService(phone string) (ret map[string]interface{}, err error) {
	params := util.GetParams(`{"uuid":"` + util.MERID + phone + `"}`)

	resp, err := http.Post(util.ADDR + `/api/maker_auth_info.api`, "application/json", params)
	if err != nil {
		return ret, err
	}

	defer resp.Body.Close()

	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ret, err
	}

	respString := string(respBody)

	ret = make(map[string]interface{})

	err = json.Unmarshal([]byte(respString), &ret)
	if err != nil {
		return ret, err
	}

	return ret, nil
}

func GetMakerAuthUrlService(phone string, returnUrl string) (ret map[string]interface{}, err error) {
	params := util.GetParams(`{"uuid":"` + util.MERID + phone + `","again":0,"is_mini_program":0,"return_url":"` + returnUrl +`"}`)

	resp, err := http.Post(util.ADDR + `/api/maker_auth_url.api`, "application/json", params)
	if err != nil {
		return ret, err		
	}

	defer resp.Body.Close()

	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ret, err
	}

	respString := string(respBody)

	ret = make(map[string]interface{})

	err = json.Unmarshal([]byte(respString), &ret)
	if err != nil {
		return ret, err
	}

	return ret, nil
}

func GetMakerSignUrlService(phone string, returnUrl string) (ret map[string]interface{}, err error) {
	params := util.GetParams(`{"uuid":"` + util.MERID + phone + `","channel_id":0,"return_url":"` + returnUrl +`"}`)

	resp, err := http.Post(util.ADDR + `/api/maker_sign_url.api`, "application/json", params)
	if err != nil {
		return ret, err		
	}

	defer resp.Body.Close()

	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ret, err
	}

	respString := string(respBody)

	ret = make(map[string]interface{})

	err = json.Unmarshal([]byte(respString), &ret)
	if err != nil {
		return ret, err
	}

	return ret, nil
}